<?php

namespace app\api\controller;

use think\Controller;
use think\facade\App;

class User extends Basic
{
    public function registerMemberm()
    {
        $post = input('post.');

        if (empty($post['membermobile'])) {
            return $this->json($post, 2000, '请输入手机号码');
        }

        if (empty($post['memberpassword'])) {
            return $this->json($post, 2000, '请输入密码');
        }

        $where = ['membermobile' => $post['membermobile']];
        $info = $this->findTable('member', ['where' => $where]);

        $data = [
            'status' => 1,
            'membername' => $post['membermobile'],
            'memberavatar' => empty($post['memberavatar']) ? \think\facade\Request::domain(true) . '/public/static/member.png' : $post['memberavatar'],
            'memberpassword' => md5($post['memberpassword']),
            'membercityid' => empty($post['membercityid']) ? '' : $post['membercityid'],
            'memberareaid' => empty($post['memberareaid']) ? '' : $post['memberareaid'],
            'membertownid' => empty($post['membertownid']) ? '' : $post['membertownid'],
            'memberaddressinfo' => empty($post['memberaddressinfo']) ? '' : $post['memberaddressinfo'],
            'membersex' => empty($post['membersex']) ? '' : $post['membersex'],
        ];

        $name = rand_str();

        if (empty($info)) {
            $data['membermobile'] = $post['membermobile'];
            $data['membernickname'] = $name;
            $data['membertruename'] = $name;
            $ans = $this->insertTable('member', $data);
        } else {
            if ($info['status'] == 1) {
                return $this->json([], 2000, '该用户已存在，请登录');
            } else if ($info['status'] == 2) {
                $where = ['memberid' => $info['memberid']];
                $update = [
                    'where' => $where,
                    'data' => $data,
                ];

                $ans = $this->updateTable('member', $update);
            } else if ($info['status'] == 0) {
                return $this->json([], 2000, '该用户已被禁止使用，请联系客服');
            } else if ($info['status'] == 4) {
                return $this->json([], 2000, '该用户已被暂停使用，请联系客服');
            }
        }

        if ($ans) {
            return $this->json($ans, 1001, '注册成功');
        } else {
            return $this->json($ans, 2000, '注册失败');
        }
    }

    public function registerCoach()
    {
        $post = input('post.');

        if (empty($post['coachmobile'])) {
            return $this->json($post, 2000, '请输入手机号码');
        }

        if (empty($post['coachpassword'])) {
            return $this->json($post, 2000, '请输入密码');
        }

        if (empty($post['coachpassword'])) {
            return $this->json($post, 2000, '请输入密码');
        }

        $where = ['coachmobile' => $post['coachmobile']];
        $info = $this->findTable('coach', ['where' => $where]);

        $name = rand_str();

        $data = [
            'status' => 1,
            'coachname' => empty($post['coachname']) ? $post['coachmobile'] : $post['coachname'],
            'coachnickname' => $name,
            'coachtruename' => $name,
            'coachavatar' => empty($post['coachtruename']) ? \think\facade\Request::domain(true) . '/public/static/coach.png' : $post['coachavatar'],
            'coachpassword' => md5($post['coachpassword']),
            'coachcityid' => empty($post['coachcityid']) ? '' : $post['coachcityid'],
            'coachareaid' => empty($post['coachareaid']) ? '' : $post['coachareaid'],
            'coachtownid' => empty($post['coachtownid']) ? '' : $post['coachtownid'],
            'coachaddressinfo' => empty($post['coachaddressinfo']) ? '' : $post['coachaddressinfo'],
            'coachsex' => empty($post['coachsex']) ? '' : $post['coachsex'],
        ];

        if (empty($info)) {
            $data['coachmobile'] = $post['coachmobile'];
            $ans = $this->insertTable('coach', $data);
        } else {
            if ($info['status'] > 0) {
                return $this->json([], 2000, '该用户已存在，请登录');
            } else if ($info['status'] == 0) {
                return $this->json([], 2000, '该用户已被禁止使用，请联系客服');
            }
        }

        if ($ans) {
            return $this->json($ans, 1001, '注册成功');
        } else {
            return $this->json($ans, 2000, '注册失败');
        }
    }

    public function login()
    {
        $post = input('post.');

        if (empty($post['mobile'])) {
            return $this->json($post, 2000, '请输入手机号码');
        }

        if (empty($post['password'])) {
            return $this->json($post, 2000, '请输入密码');
        }

        $type = empty($post['type']) || !in_array($post['type'], [1, 2]) ? 1 : $post['type'];

        if ($type == 1) {
            $where = ['membermobile' => $post['mobile']];
            $table = 'member';
        } else {
            $where = ['coachmobile' => $post['mobile']];
            $table = 'coach';
        }

        $info = $this->findTable($table, ['where' => $where]);
        $isbool = 0;
        if (empty($info)) {
            return $this->json([], 2000, '账户不存在');
        } else {
            if ($type == 1) {
                if ($info['status'] == 0) {
                    return $this->json([], 2000, '该用户已被禁止使用，请联系客服');
                }else if($info['status'] == 2) {
                    return $this->json([], 2000, '当前账号未激活，请前往注册页面进行注册激活');
                } else if ($info['status'] == 4) {
                    if ($info['lockingtime'] != 1) {
                        if ($info['lockingtime'] >= time()) {
                            $where == ['memberid' => $info['memberid']];
                            $update = [
                                'where' => $where,
                                'data' => ['status' => 1, 'lockingtime' => 0],
                            ];
                            $id = $info['memberid'];
                            $this->updateTable('member', $update);
                        }
                    } else {
                        return $this->json([], 2000, '该用户已被暂停使用，请联系客服');
                    }
                }
                if ($info['memberpassword'] != md5($post['password'])) {
                    return $this->json([], 2000, '账号密码错误');
                }
                $id = $info['memberid'];
            } else {
                if ($info['status'] == 0) {
                    return $this->json([], 2000, '该用户已被禁止使用，请联系客服');
                }else if ($info['status'] == 5) {
                    if ($info['lockingtime'] != 1) {
                        if ($info['lockingtime'] >= time()) {
                            $where = ['coachid' => $info['coachid']];
                            $update = [
                                'where' => $where,
                                'data' => ['status' => 3, 'lockingtime' => 0],
                            ];
                            $id = $info['coachid'];
                            $this->updateTable('coach', $update);
                        }
                    } else {
                        return $this->json([], 2000, '该用户已被暂停使用，请联系客服');
                    }
                }

                if ($info['coachpassword'] != md5($post['password'])) {
                    return $this->json([], 2000, '账号密码错误');
                }
                if ($info['status'] == 1) {
                    $isbool = 1;
                }
                
                $id = $info['coachid'];
            }
        }

        if (empty($info['yxaccid'])) {
            if ($type == 1) {
                $where = ['memberid' => $info['memberid']];
                $name = $info['membertruename'];
                $id = $info['memberid'];
            } else {
                $where = ['coachid' => $info['coachid']];
                $name = $info['coachtruename'];
                $id = $info['coachid'];
            }

            $yx = new \app\common\lib\YxUtils();
            $res = $yx->createUserId($table . '_' . $id, $name);
            if ($res['code'] == 200) {
                $update = [
                    'where' => $where,
                    'data' => [
                        'yxaccid' => empty($res['info']['accid']) ? '' : $res['info']['accid'],
                        'yxtoken' => empty($res['info']['token']) ? '' : $res['info']['token']
                    ],
                ];
                $this->updateTable($table, $update);
            }
        }

        $token = $this->getToken($table, $info, $id);
        if ($token) {
            return $this->json(['token' => $token, 'isbool' => $isbool], 1000);
        } else {
            return $this->json([], 2000, '登录失败');
        }
    }

    /**
     * 用户端修改密码
     */
    public function editPassword()
    {
        $post = input('post.');

        if (empty($post['mobile'])) {
            return $this->json($post, 2000, '请输入手机号码');
        }

        if (empty($post['password'])) {
            return $this->json($post, 2000, '请输入密码');
        }

        $type = empty($post['type']) || !in_array($post['type'], [1, 2]) ? 1 : $post['type'];

        if ($type == 1) {
            $where = ['membermobile' => $post['mobile']];
            $table = 'member';
        } else {
            $where = ['coachmobile' => $post['mobile']];
            $table = 'coach';
        }

        $info = $this->findTable($table, ['where' => $where]);

        if (empty($info)) {
            return $this->json([], 2000, '账户不存在');
        } else {
            if ($type == 1) {
                $where == ['memberid' => $info['memberid']];
                $update = [
                    'where' => $where,
                    'data' => ['memberpassword' => md5($post['password'])],
                ];
                $id = $info['memberid'];
                $ans = $this->updateTable('member', $update);
            } else {
                $where = ['coachid' => $info['coachid']];
                $update = [
                    'where' => $where,
                    'data' => ['coachpassword' => md5($post['password'])],
                ];
                $id = $info['coachid'];
                $ans = $this->updateTable('coach', $update);
            }

            $update = [
                'where' => [
                    'table' => $table,
                    'status' => 1,
                    'userid' => $id,
                ],
                'data' => ['status' => 0],
            ];

            $ans = $this->updateTable('user_login', $update);

            if ($ans) {
                return $this->json($ans, 1001, '密码修改成功');
            } else {
                return $this->json($ans, 2000, '密码修改失败');
            }
        }
    }
}
